import QtQuick 2.6
import Sailfish.Silica 1.0
import Sailfish.Pickers 1.0
import "../config/Dimens.js" as AppDimens
import "../component"
import "../config"


Item {
    id: homePage
    property var novelList: []

    signal novelImported(string finishNovelName)

    //组件加载完成，触发数据加载
    Component.onCompleted: {
        flashNovelList()
        novelImported("测试信号和槽")
        novelService.novelImported.connect(onNovelImported)
        novelService.importError.connect(onImportError)
    }

    //刷新小说列表函数
    function flashNovelList(){
        homePage.novelList = novelService.getAllNovels()
    }

    //导入成功槽
    function onNovelImported(finishNovelName){
        showNotice(finishNovelName+"导入成功")
        flashNovelList()
    }

    //导入失败槽
    function onImportError(msg){
        showNotice(msg)
    }

    Column{
        width: parent.width
        height: parent.height
        leftPadding: 10
        rightPadding: 10
        topPadding: 20
        property int realWidth: width-leftPadding-rightPadding
        property int realHeight: height-topPadding-bottomPadding

        //顶部区域：搜索按钮
        Row {
            id:searchContainer
            width: parent.width
            height: 120

            //文字时钟
            UITextClock {
                anchors.left: parent.left
                anchors.verticalCenter: parent.verticalCenter
                textColor: AppColor.themeMode.white
            }

            Label {
                id:addLabel
                text: qsTr("+")
                color: AppColor.themeMode.thirdColor
                font.pixelSize: Theme.fontSizeHuge
                font.bold: true
                anchors.verticalCenter: parent.verticalCenter
                anchors.right: searchRect.left
                anchors.rightMargin: 80


                MouseArea {
                    anchors.fill: parent
                    onClicked: {
                        pageStack.push(filePickerPage)
                    }
                }
            }

            FilePickerPage {
                id:filePickerPage
                nameFilters: [ '*.txt' ]
                title: "导入小说"
                onSelectedContentPropertiesChanged: {
                    if (selectedContentProperties && selectedContentProperties.filePath) {
                        console.log("File path:", selectedContentProperties.filePath)
                        novelService.importFromPath(selectedContentProperties.filePath)
                    } else {
                        console.error("Error: filePath is null or undefined")
                        showNotice("导入失败：未选择有效文件")
                    }
                }
            }

            //搜索框
            Rectangle{
                id: searchRect
                height: 80
                width: searchTitle.contentWidth+100
                radius: 20
                anchors.right: parent.right
                anchors.rightMargin: 40
                color: AppColor.themeMode.primaryComponentColor
                anchors.verticalCenter: parent.verticalCenter

                MouseArea{
                    anchors.fill: parent
                    onClicked: {
                        pageStack.animatorPush(Qt.resolvedUrl("SearchPage.qml"),{}, PageStackAction.Immediate)
                    }
                }

                Text {
                    id:searchTitle
                    text: qsTr("搜索")
                    color: AppColor.themeMode.thirdColor
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.left: parent.left
                    anchors.leftMargin: 10
                }

                Icon {
                    source: "../img/icon_search.svg"
                    width: 80
                    height: 80
                    fillMode: Image.PreserveAspectFit
                    color: AppColor.themeMode.primaryColor
                    anchors.left: searchTitle.right
                }
            }
        }

        Row{
            height: 40
            width: parent.realWidth
        }

        //书架区域
        SilicaFlickable{
            width: parent.realWidth
            height: parent.realHeight-searchContainer.height-40
            contentHeight: bookContainerGrid.height
            clip: true

            VerticalScrollDecorator {}

            Grid{
                id:bookContainerGrid
                property int bookWidth: AppDimens.bookWidth
                property int maxColumnCount: parent.width/bookWidth
                property int columnCount: parent.width-maxColumnCount*bookWidth-(maxColumnCount-1)*20>=0?maxColumnCount:maxColumnCount-1

                columns: columnCount
                width: columnCount*bookWidth+(columnCount-1)*20
                columnSpacing: (parent.width-parent.width/bookWidth*bookWidth)/(parent.width/bookWidth-1)>20?(parent.width-parent.width/bookWidth*bookWidth)/(parent.width/bookWidth-1):20
                rowSpacing: 20
                anchors.centerIn: parent

                Repeater{
                    model: homePage.novelList

                    delegate: UIBookTile {
                        viewWidth: (bookContainerGrid.width - bookContainerGrid.columns * bookContainerGrid.columnSpacing) / bookContainerGrid.columns
                        viewHeight: width*1.4
                        novelNameValue: modelData.title

                        MouseArea{
                            anchors.fill: parent
                            onClicked: {
                                console.log("阅读小说："+modelData.id);
                                pageStack.animatorPush(
                                    Qt.resolvedUrl("ReadPage.qml"),
                                    {
                                        novelId: modelData.id,  // 传递小说ID
                                        novelTitle: modelData.title  // 可选：传递小说标题
                                    },
                                    PageStackAction.Immediate
                                )
                            }
                        }

                    }
                }

            }


        }
    }
}
